package com.link.base.base.invloc.controller;

import com.link.base.base.invloc.model.InvProd;
import com.link.base.base.invloc.model.Invloc;
import com.link.base.base.invloc.service.InvProdService;
import com.link.base.base.invloc.service.InvlocService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.ServiceException;
import com.link.core.modules.export.ExportFactory;
import com.link.core.modules.export.ExportWriter;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品库存信息控制器
 * @author: unknown
 * created: unknown
 * comments:
 */
@Controller
@RequestMapping("/link/invPro")
public class InvProdController extends BasicController<InvProd> {
    @Resource
    private InvProdService invProdService;
    @Resource
    private InvlocService invlocService;

    @Override
    public BasicService<InvProd> getBasicService() throws Exception {
        return invProdService;
    }

    @RequestMapping(value = "/queryByExamplePage/{id}")
    @ResponseBody
    public Map<String, Object> List(@PathVariable Long id,@JsonParam QueryParams qps, HttpSession session,
                                    HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        qps.addFilter(new Filter("id", String.valueOf(id)));
        InvProd record = new InvProd();
        record.setInvlocId(id);
        List<InvProd> list = null;
        try {
            list = invProdService.queryInvProdByInvId(record);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", qps.getTotal());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<InvProd>());
            result.put("result", e.getMessage());
        }
        return result;

    }

    /**
     * <p>
     * 获取经销商产品库存信息:用户id-经销商id-库存id-经销商产品信息和库存信息
     * </p>
     *
     * @author guoshan
     * @param qps
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/queryInvProdPage")
    @ResponseBody
    public Map<String, Object> queryProAndInvtxn(@JsonParam QueryParams qps, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            qps.preDealSecurity(request);
            Long acctId = UserUtil.getUser().getAcctId();
            if (null == acctId) {
                throw new ServiceException("PUBLIC-026");
            }
            Invloc invloc = new Invloc();
            invloc.setOwnerId(acctId);
            invloc.setTotalFlag(true);
            invloc.setPageFlag(false);
            List<Invloc> invLocs = invlocService.queryByExamplePage(invloc);
            if (invLocs.isEmpty()) {
                result.put("success", false);
                result.put("result", "当前客户没有仓库");
                return result;
            }
            InvProd invProd = new InvProd();
            invProd.setInvlocId(invLocs.get(0).getId());
            invProd.setAttr1(qps.getAttr1());
            qps.addFilter(new Filter("headId10123213133", "invlocId", Filter.OP_EQUAL, String.valueOf(invProd.getInvlocId())));
            return this.queryByExamplePage(qps, invProd, session, request, response);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 导出 excel 数据
     */

    @Override
    @RequestMapping(value = "/exportExcelData")
    @ResponseBody
    public Map<String, Object> exportData(@JsonParam InvProd entity,@JsonParam QueryParams qps, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> result = new HashMap<String, Object>();
        try {


            ExportWriter exportWriter;
            // xlsx
            exportWriter = ExportFactory.Excel2007Instance(qps);

            qps.preDealSecurity(request);
            Long acctId = UserUtil.getUser().getAcctId();
            if (null == acctId) {
                throw new ServiceException("PUBLIC-026");
            }
            Invloc invloc = new Invloc();
            invloc.setOwnerId(acctId);
            invloc.setTotalFlag(true);
            invloc.setPageFlag(false);
            List<Invloc> invLocs = invlocService.queryByExamplePage(invloc);
            if (invLocs.isEmpty()) {
                result.put("success", false);
                result.put("result", "当前客户没有仓库");
                return result;
            }
            InvProd invProd = new InvProd();
            invProd.setInvlocId(invLocs.get(0).getId());
            qps.addFilter(new Filter("headId10123213133", "invlocId", Filter.OP_EQUAL, String.valueOf(invProd.getInvlocId())));
            InvProd t = (InvProd) BasicModel.transformClass(invProd, qps);
            t.setPageFlag(false);
            while (!qps.getStopExport()) {
                beforQueryAllOrExample(t, request);
                beforQueryExample(t, request);
                List<InvProd> list = invProdService.queryByExamplePage(t);
                exportWriter.wirteData(list);
                t.setTotalFlag(qps.getTotalFlag());
            }
            result.put("success", true);
            result.put("url", exportWriter.getUrlPath());
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
